/**
 * 解析字符串数组，返回结果集
 * @param {*} strArr 字符串数组，每一个元素都是一个profile
 * @param {*} reg 解析profile的正则表达式
 * @param {*} type profile的类型（vlan-profile\ip-profile\sip-profile）
 * @returns 返回结果数组
 */
 function parseOltProfile(strArr,reg,type){
    let arr = [];
    for (let str of strArr) {
        let profileObj = {}
        strToProfile(str,reg,profileObj)
        let profileInfo = transfer(profileObj,type);
        if(profileInfo){
            arr.push({
                _id:"",
                name:profileInfo.profileName,
                type:type,
                context:JSON.stringify(profileInfo)
            })
        }
    }
    return arr;
}
/**
 * 解析字符串数组，返回结果集
 * @param {*} strArr 字符串数组，每一个元素都是一个profile
 * @param {*} reg 解析profile的正则表达式
 * @param {*} type WANIP 或 OIP
 */
function parseOnuWan(strArr,reg){
    let arr = [];
    for (let str of strArr) {
        let profileObj = {}
        strToProfile(str,reg,profileObj)
        let profileInfo = transfer(profileObj,'WANIP');
        if(profileInfo){
            arr.push(profileInfo)
        }
    }
    return arr;
}

function parseEth(strArr,reg){
    let EthMap = {};
    for(let str of strArr){
        let eth = {};
        strToProfile(str,reg,eth);
        let ethInfo = transfer(eth,'ETH');
        if(ethInfo){
            EthMap[ethInfo._interface] = ethInfo
        }
    }
    return EthMap;
}
function createAttribute(attribute, profileObj) {
    let [key, value] = attribute;
    profileObj[key] = value;
}

function transfer(profileObj,type) {
    let result = {};
    if(Object.keys(profileObj).length===0){
        return null;
    }
    let fn = createTransfer(type);
    return fn(profileObj);
}
function createTransfer(type){
    let fn;
    switch(type){
        case "vlan-profile":
            fn = function(profileObj){
                let result = {};
                for (let key in profileObj) {
                    switch (key) {
                        case "Profile name":
                            result["profileName"] = profileObj[key];
                            break;
                        case "Tag mode":
                            result["tagMode"] = profileObj[key];
                            break;
                        case "CVLAN":
                            result["cVlan"] = profileObj[key];
                            break;
                        case "CVLAN priority":
                            result["cVlanPriority"] = profileObj[key];
                            break;
                        case "SVLAN":
                            result["sVlan"] = profileObj[key];
                            break;
                    }
                }
                return result;
            }
            break;
        case "ip-profile":
            fn=function(profileObj){
                let result={}
                for (let key in profileObj) {
                    switch (key) {
                        case "Profile name":
                            result["profileName"] = profileObj[key];
                            break;
                        case "Gateway":
                            result["gateway"] = profileObj[key];
                            break;
                        case "Primary DNS":
                            result["primaryDNS"] = profileObj[key];
                            break;
                        case "Secondary DNS":
                            result["secondaryDNS"] = profileObj[key];
                            break;
                    }
                }
                return result;
            }
            break;
        case "sip-profile":
            fn=function(profileObj){
                let result={};
                for (let key in profileObj) {
                    switch (key) {
                        case "Profile name":
                            result["profileName"] = profileObj[key];
                            break;
                        case "Proxy server":
                            result["proxyServer"] = profileObj[key];
                            break;
                        case "Outbound proxy":
                            result["outboundProxy"] = profileObj[key];
                            break;
                        case "Registrar":
                            result["registrar"] = profileObj[key];
                            break;
                        case "Validation scheme":
                            result["validationScheme"] = profileObj[key];
                            break;
                        case "UDP port":
                            result["udpPort"] = profileObj[key];
                            break;
                        case "DSCP/TOS":
                            result["dscpTos"] = profileObj[key];
                            break;
                        case "Media UDP port":
                            result["mediaUdpPort"] = profileObj[key];
                            break;
                        case "Media DSCP/TOS":
                            result["mediaDscpTos"] = profileObj[key];
                            break;
                        case "DNS1:":
                            result["dns1"] = profileObj[key];
                            break;
                        case "DNS2":
                            result["dns2"] = profileObj[key];
                            break;
                        case "Registration expiration time":
                            result["registrationExpirationTime"] = profileObj[key];
                            break;
                        case "Re-registration time":
                            result["reRegistrationTime"] = profileObj[key];
                            break;
                        case "User domain":
                            result["userDomain"] = profileObj[key];
                            break;
                        case "Softswitch vendor":
                            result["softswitchVendor"] = profileObj[key];
                            break;
                        case "Dial plan table name":
                            result["dialPlanTableName"] = profileObj[key];
                            break;
                        case "Release timer":
                            result["releaseTimer"] = profileObj[key];
                            break;
                        case "ROH timer":
                            result["rohTimer"] = profileObj[key];
                            break;
                        case "Link test":
                            result["linkTest"] = profileObj[key];
                            break;
                        case "Link test interval":
                            result["linkTestInterval"] = profileObj[key];
                            break;
                        case "Appsrv":
                            result["appsrv"] = profileObj[key];
                            break;
                        case "Accesscode":
                            result["accesscode"] = profileObj[key];
                            break;
                    }
                }
                return result;
            }
            break;
        case "WANIP":
            fn = function(profileObj){
                let result = {};
                for (let key in profileObj) {
                    switch (key) {
                        case "/WAN in?dex/i":
                            result["id"] = profileObj[key];
                            break;
                        case "Mode":
                            result["mode"] = profileObj[key];
                            break;
                        case "ONU identifier":
                            result["onuIdentifier"] = profileObj[key];
                            break;
                        case "Ping response":
                            result["pingResponse"] = profileObj[key];
                            break;
                        case "Traceroute response":
                            result["tracerouteResponse"] = profileObj[key];
                            break;
                        case "VLAN tag mode":
                            result["vlanTagMode"] = profileObj[key];
                            break;
                        case "CVLAN":
                            result["cVlan"] = profileObj[key];
                            break;
                        case "CVLAN priority":
                            result["cVlanPriority"] = profileObj[key];
                            break;
                        case "SVLAN":
                            result["sVlan"] = profileObj[key];
                            break;
                        case "Current IP":
                            result["currentIP"] = profileObj[key];
                            break;
                        case "Current mask":
                            result["currentMask"] = profileObj[key];
                            break;
                        case "Current gateway":
                            result["gateway"] = profileObj[key];
                            break;
                        case "Current primary DNS":
                            result["currentPrimaryDNS"] = profileObj[key];
                            break;
                        case "Current secondary DNS":
                            result["currentSecondaryDNS"] = profileObj[key];
                            break;
                        case "Domain name":
                            result["domainName"] = profileObj[key];
                            break;
                        case "Host name":
                            result["hostName"] = profileObj[key];
                            break;
                        case "MAC address":
                            result["mac"] = profileObj[key];
                            break;
                        case "IP host ID":
                            result["host"] = profileObj[key];
                            break;
                    }
                }
                return result;
            }
            break;
        case "VOIP":
            break;
        case "ETH":
            fn=function(profileObj){
                let result = {};
                for (let key in profileObj) {
                    switch (key) {
                        case "Interface":
                            result["_interface"] = profileObj[key];
                            break;
                        case "Speed status":
                            result["speedStatus"] = profileObj[key];
                            break;
                        case "Operate status":
                            result["operateStatus"] = profileObj[key];
                            break;
                        case "Admin status":
                            result["adminStatus"] = profileObj[key];
                            break;
                        case "ARC":
                            result["arc"] = profileObj[key];
                            break;
                        case "ARC interval":
                            result["arcInterval"] = profileObj[key];
                            break;
                        case "Expect type":
                            result["expectType"] = profileObj[key];
                            break;
                        case "Speed config":
                            result["speedConfig"] = profileObj[key];
                            break;
                        case "Ethernet loop":
                            result["ethernetLoop"] = profileObj[key];
                            break;
                        case "Max frame":
                            result["maxFrame"] = profileObj[key];
                            break;
                        case "Pause time":
                            result["pauseTime"] = profileObj[key];
                            break;
                        case "Wiring":
                            result["wiring"] = profileObj[key];
                            break;
                        case "Bridge or IP":
                            result["bridgeOrIP"] = profileObj[key];
                            break;
                        case "PPPoE filter":
                            result["pppoeFilter"] = profileObj[key];
                            break;
                        case "Power control":
                            result["powerControl"] = profileObj[key];
                            break;
                        case "Status changes":
                            result["statusChanges"] = profileObj[key];
                            break;
                    }
                }
                return result;
            }
            break;
    }
    return fn;
}
/**
 * 把字符串按照分隔符分割，返回一个数组。
 * @param {*} str 
 * @param {*} spliter 分隔符
 * @returns 
 */
function strToEachType(str,spliter){
    return str.split(spliter).map(item => spliter + item);
}

/**
 * 将字符串转换成属性添加到profileObj上
 * @param {*} str 
 * @param {*} reg 正则表达式 用于从str中过滤出 key->value
 * @param {*} profileObj 正则表达式过滤出的key->value将作为属性添加到此对象上
 */
function strToProfile(str,reg,profileObj){
    let group = str.match(reg);
    if(group && group.length > 1){
        group.forEach(item => {
            if (item.indexOf('\n') !== -1) {
                item.split('\n').forEach(line => {
                    let attribute = line.split(':').map(e => e.trim());
                    if (attribute.length == 2) {
                        createAttribute(attribute, profileObj)
                    }
                })
            } else {
                let attribute = item.split(':').map(e => e.trim());
                if (attribute.length == 2) {
                    createAttribute(attribute, profileObj)
                }
            }
        })
    }
}


module.exports = {
    parseOltProfile,
    strToEachType,
    parseOnuWan,
    parseEth
}